from base.basePage import BasePage
from selenium.webdriver.common.by import By
from selenium import webdriver
import time
from page import MailListPageElement
from config import *
import allure
from page import MailListPageElement


class MailListPage(BasePage):

    def __init__(self, driver):
        super().__init__(driver)

    def get_mail_list_page(self):
        "进入通讯录页面"
        self.get_url(mail_list_url)

    def add_org(self, org_name='', parent_org=None):
        """
        添加部门-业务操作步骤
        """
        # 点击+号
        with allure.step("点击+号"):
            self.my_click(MailListPageElement.add_button)
        # 点击-添加部门
        with allure.step("点击-添加部门"):
            self.my_click(MailListPageElement.add_org_button)
        # 输入部门名称
        if org_name:
            with allure.step("输入部门名称：{}".format(org_name)):
                self.my_send_keys(MailListPageElement.org_name_input, org_name)
        # 判断,所属部门为空时,不选择所部门.
        if parent_org:
            parent_org_choice = (MailListPageElement.parent_org_choice[0], MailListPageElement.parent_org_choice[-1].format(parent_org))
            # 点击-选择所属部门,展开下拉选择框
            with allure.step("点击-选择所属部门:{}".format(parent_org)):
                self.my_click(MailListPageElement.parent_org_select)
                # 选择上级部门-啦啦啦啦
                self.my_click(parent_org_choice)
        # 点击-确定
        # time.sleep(5)
        with allure.step("点击-确定"):
            self.my_click(MailListPageElement.define_button)

    def update_org(self, old_name, new_name=''):
        """
        编辑组织-业务操作
        :return:
        """
        # 找到部门,点击...
        with allure.step("点击部门名称旁..."):
            new_choice_org = (MailListPageElement.choice_org[0], MailListPageElement.choice_org[-1].format(old_name))
            new_choice_org_button = (MailListPageElement.choice_org_button[0], MailListPageElement.choice_org_button[-1].format(old_name))
            self.my_click(new_choice_org)
            self.my_click(new_choice_org_button)
        # 点击-修改名称
        with allure.step("点击-修改名称按钮".format(new_name)):
            self.my_click(MailListPageElement.update_name_button)
        # 输入需要修改的名称
        with allure.step("输入部门名称：{}".format(new_name)):
            self.clear_input(MailListPageElement.org_name_input)
            if new_name:
                self.my_send_keys(MailListPageElement.org_name_input, new_name)
        # 点击-确定
        with allure.step("点击-保存"):
            self.my_click(MailListPageElement.define_button)



# # 初始化浏览器对象
# driver = webdriver.Chrome("C:\Program Files\driver\chromedriver.exe")
# driver.maximize_window()
# driver.get("https://work.weixin.qq.com/wework_admin/frame#index")
#
# time.sleep(10)
# # from config import *
# # # 浏览器中添加cookie
# # for cookie in cookies:
# #     driver.add_cookie(cookie)
# # 初始化页面对象
# mail_list_page = MailListPage(driver)
# # 打开通讯录页面
# mail_list_page.get_mail_list_page()
# # 添加组织
# mail_list_page.add_org(org_name="坚果的测试部门", parent_org="啦啦啦啦")
#
# time.sleep(5)
# print(mail_list_page.my_get_cookies())
# # mail_list_page.my_quit()